<?php use_stylesheet('smoothness/jquery-ui-1.7.2.custom.css') ?>

<h1>Listado de Reservas</h1>
<div id="MyDatepicker"></div>

<script type="text/javascript">

	$(function() {
            $.datepicker.setDefaults($.datepicker.regional['es']);
		$("#MyDatepicker").datepicker({
			changeMonth: true,
			changeYear: true,    
                        firstDay: 1,
                        //defaultDate: +7,
                        onSelect: function(dateText, inst){
                        var theDate = new Date(Date.parse($(this).datepicker('getDate')));
                        var dateFormatted = $.datepicker.formatDate('yy-mm-dd', theDate);
                        $("#mydate").val(dateFormatted);
                    }
		});

	});
</script>

<form action="<?php echo url_for('reserva/listarxdia'); ?>" method="post">
<table>
<tbody>
<?php echo $formulario; ?>
<input type="hidden" name="mydate" id="mydate" />
</tbody>
</table>

<input type="submit" value=" BUSCAR " name="save"/>
</form>
<br/>

<?php $horario = array(); ?>
<?php $box = array(); ?>
<?php $data = array(); ?>
<?php $reservas = array(); ?>

<?php //MATRIZ HORARIO VACIA ?>
<?php foreach ($gestdent_hora_list as $gestdent_hora): ?>
    <?php foreach ($gestdent_box_list as $gestdent_box): ?>
        <?php $data["Doctor"] = ""; ?>
        <?php $data["Paciente"] = ""; ?>
        <?php $data["Color"] = ""; ?>
        <?php $box[$gestdent_box->getId()] = $data; ?>
        <?php $horario[$gestdent_hora->getId()] = $box; ?>
    <?php endforeach; ?>
<?php endforeach; ?>

<?php //LLENAMOS MATRIZ DE RESERVAS ?>
<?php foreach ($gestdent_reserva_list as $gestdent_reserva): ?>
    <?php $reservas[$gestdent_reserva->getPersonal()][$gestdent_reserva->getHora()]["ID_PACIENTE"] = $gestdent_reserva->getPaciente() ?>
    <?php $reservas[$gestdent_reserva->getPersonal()][$gestdent_reserva->getHora()]["PACIENTE"] = "Paciente: ".$gestdent_reserva->getGestdentPaciente()->getNombres()." ".$gestdent_reserva->getGestdentPaciente()->getApellidoPaterno() ?>
    <?php $reservas[$gestdent_reserva->getPersonal()][$gestdent_reserva->getHora()]["ID_RESERVA"] = $gestdent_reserva->getId() ?>
<?php endforeach; ?>

<?php //LLENAMOS MATRIZ CON HORARIOS ?>
<?php foreach ($gestdent_horario3_list as $gestdent_horario3): ?>

    <?php $detalle_list = $gestdent_horario3->getDetalle() ?>
    <?php foreach ($detalle_list as $detalle): ?>
        <?php $horario[$detalle->getHoraId()][$gestdent_horario3->getBoxId()]["Doctor"] .= "Doctor: ".$gestdent_horario3->getGestdentPersonal()->getNombres()." ".$gestdent_horario3->getGestdentPersonal()->getApellidoPaterno()."<br>" ?>
        <?php $horario[$detalle->getHoraId()][$gestdent_horario3->getBoxId()]["Doctor_id"] = $gestdent_horario3->getGestdentPersonal()->getId() ?>
        <?php $mipaciente = null; ?>
        <?php try{$mipaciente = $reservas[$gestdent_horario3->getPersonalId()][$detalle->getHoraId()]["PACIENTE"];} catch(Exception $e){} ?>
        <?php $horario[$detalle->getHoraId()][$gestdent_horario3->getBoxId()]["Paciente"] .= $mipaciente."<br>" ?>
        <?php try{$mireserva = $reservas[$gestdent_horario3->getPersonalId()][$detalle->getHoraId()]["ID_RESERVA"];} catch(Exception $e){} ?>
        <?php $horario[$detalle->getHoraId()][$gestdent_horario3->getBoxId()]["Reserva_id"] = $mireserva ?>

    <?php endforeach; ?>

<?php endforeach; ?>


<table>
    <thead>
        <th><?php echo "Hora/Box" ?></th>
        <?php foreach ($gestdent_box_list as $gestdent_box): ?>
        <th><?php echo $gestdent_box->getNombre() ?></th>
        <?php endforeach; ?>
    </thead>

    <tbody>
        <?php foreach ($gestdent_hora_list as $gestdent_hora): ?>
        <tr>
            <td>
                <?php echo $gestdent_hora->getDescripcion() ?>
            </td>

            <?php foreach ($gestdent_box_list as $gestdent_box): ?>
            <td>
                <?php echo $horario[$gestdent_hora->getId()][$gestdent_box->getId()]["Doctor"]; ?>
                <?php echo $horario[$gestdent_hora->getId()][$gestdent_box->getId()]["Paciente"]; ?>

                <?php if($horario[$gestdent_hora->getId()][$gestdent_box->getId()]["Doctor"] != ""): ?>
                    <?php if($horario[$gestdent_hora->getId()][$gestdent_box->getId()]["Paciente"] == "<br>"): ?>

                        <form action="<?php echo url_for('reserva/save2')?>" method="post">
                        <input type="hidden" name="hora_id" value="<?php echo $gestdent_hora->getId()?>" />
                        <input type="hidden" name="Doctor_id" value="<?php echo $horario[$gestdent_hora->getId()][$gestdent_box->getId()]["Doctor_id"] ?>" />
                        <input value="RESERVAR" type="submit" />
                        </form>
                

                    <?php else: ?>

                        <!--<form action="<?php //echo url_for('reserva/save')?>" method="post">
                        <input type="hidden" name="hora_id" value="<?php //echo $gestdent_hora->getId()?>" />
                        <input type="hidden" name="Doctor_id" value="<?php //echo $horario[$gestdent_hora->getId()][$gestdent_box->getId()]["Doctor_id"] ?>" />
                        
                        <input type="image" src="../../images/tick.png" />
                        </form>-->
                        
                        <a href="<?php echo url_for('reserva/show?id='.$horario[$gestdent_hora->getId()][$gestdent_box->getId()]["Reserva_id"]) ?>">Ver</a>
                        <a href="<?php echo url_for('reserva/edit?id='.$horario[$gestdent_hora->getId()][$gestdent_box->getId()]["Reserva_id"]) ?>">Actualizar</a>

                    <?php endif; ?>

                <?php endif; ?>

            </td>
            <?php endforeach; ?>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>